package com.amado.course35Concurrent;

import java.util.Date;
import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created by Administrator on 2018\3\18 0018.
 */
public class S14Callable {
	public static void main(String[] args) throws ExecutionException, InterruptedException {
		userFuture();
	}
	public static int userFuture() throws ExecutionException, InterruptedException {
//		ExecutorService executorService = Executors.newFixedThreadPool(2);
		//初始化2个线程，调用 submit如果线程不够就加一个，但最多是8个，超8个线程报异常，闲置时间5分钟就回收线程
		ExecutorService executorService = new ThreadPoolExecutor(2, 8, 5, TimeUnit.MINUTES,
				new LinkedBlockingDeque<Runnable>());
		// Future future = new FutureTask(new Thraed3(search));
		Future<Integer> searchFuture = executorService.submit(new Callable<Integer>() {
			@Override
			public Integer call() throws Exception {
				return new Random().nextInt(10);
			}
		});
		Integer searcData = searchFuture.get();
		executorService.shutdown();
		return searcData;
	}
}
